#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <exception>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

typedef struct TTerm {
	int type;
	string value;
	vector<TTerm> parts;
} TTerm;

class MyException : public exception {
public:
	string info;
	string line;

	virtual ~MyException() throw(){}
};

const int BNFWORD = 0;
const int COMMAND = 1;
const int COND = 2;
const int CONDITION = 3;
const int IFSTAT = 4;
const int MEMORY = 5;
const int OP = 6;
const int PROGRAM = 7;
const int STATEMENT = 8;

string trim(string s) {
	int begin = s.find_first_not_of(" \t\r\n");
	int end = s.find_last_not_of(" \t\r\n");
	return s.substr(begin, end+1-begin);
}

TTerm matchBNFWord(const string word, string& line){
	TTerm BNFWord;
	BNFWord.type = BNFWORD;

	line = trim(line);
	for (unsigned int i=0; i<word.length(); ++i) {
		if (word[i] != line[i]) {
			MyException e;
			e.info = "mismatch in BNFWord";
			e.line = line;
			throw e;
		}
	}
	line = line.substr(word.length(), line.length()-word.length());
	BNFWord.value = word;

	return BNFWord;
}

TTerm match_command(string& line);
TTerm match_cond(string& line);
TTerm match_condition(string& line);
TTerm match_ifstat(string& line);
TTerm match_memory(string& line);
TTerm match_op(string& line);
TTerm match_program(string& line);
TTerm match_statement(string& line);

TTerm match_program(string& line) {
	TTerm _program;
	TTerm t;
	_program.type = PROGRAM;

	t = match_statement(line);
	_program.parts.push_back(t);

	t = matchBNFWord(".", line);
	_program.parts.push_back(t);

	return _program;
}

TTerm match_statement(string& line) {
	TTerm _statement;
	TTerm t;
	_statement.type = STATEMENT;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _statement.parts;
		try {
			t = match_command(line);
			_statement.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_statement.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _statement.parts;
		try {
			t = match_ifstat(line);
			_statement.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_statement.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in STATEMENT";
		myE.line = line;
		throw myE;
	}
	return _statement;
}

TTerm match_ifstat(string& line) {
	TTerm _ifstat;
	TTerm t;
	_ifstat.type = IFSTAT;

	t = matchBNFWord("IF", line);
	_ifstat.parts.push_back(t);

	t = match_condition(line);
	_ifstat.parts.push_back(t);

	t = matchBNFWord("THEN", line);
	_ifstat.parts.push_back(t);

	t = match_statement(line);
	_ifstat.parts.push_back(t);

	t = matchBNFWord("ELSE", line);
	_ifstat.parts.push_back(t);

	t = match_statement(line);
	_ifstat.parts.push_back(t);

	return _ifstat;
}

TTerm match_condition(string& line) {
	TTerm _condition;
	TTerm t;
	_condition.type = CONDITION;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _condition.parts;
		try {
			t = match_cond(line);
			_condition.parts.push_back(t);

			t = match_op(line);
			_condition.parts.push_back(t);

			t = match_condition(line);
			_condition.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_condition.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _condition.parts;
		try {
			t = match_cond(line);
			_condition.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_condition.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in CONDITION";
		myE.line = line;
		throw myE;
	}
	return _condition;
}

TTerm match_op(string& line) {
	TTerm _op;
	TTerm t;
	_op.type = OP;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _op.parts;
		try {
			t = matchBNFWord("AND", line);
			_op.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_op.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _op.parts;
		try {
			t = matchBNFWord("OR", line);
			_op.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_op.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in OP";
		myE.line = line;
		throw myE;
	}
	return _op;
}

TTerm match_cond(string& line) {
	TTerm _cond;
	TTerm t;
	_cond.type = COND;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	t = match_memory(line);
	_cond.parts.push_back(t);

	hasErr = true;
	bakLn[1] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _cond.parts;
		try {
			t = matchBNFWord("=", line);
			_cond.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_cond.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _cond.parts;
		try {
			t = matchBNFWord("#", line);
			_cond.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_cond.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[1];
		myE.info = "mismatch in COND";
		myE.line = line;
		throw myE;
	}
	hasErr = true;
	bakLn[1] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _cond.parts;
		try {
			t = match_command(line);
			_cond.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_cond.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _cond.parts;
		try {
			t = matchBNFWord("NULL", line);
			_cond.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_cond.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[1];
		myE.info = "mismatch in COND";
		myE.line = line;
		throw myE;
	}
	return _cond;
}

TTerm match_memory(string& line) {
	TTerm _memory;
	TTerm t;
	_memory.type = MEMORY;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[1] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _memory.parts;
		try {
			t = matchBNFWord("MY", line);
			_memory.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_memory.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _memory.parts;
		try {
			t = matchBNFWord("YOUR", line);
			_memory.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_memory.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[1];
		myE.info = "mismatch in MEMORY";
		myE.line = line;
		throw myE;
	}
	t = matchBNFWord("LAST", line);
	_memory.parts.push_back(t);

	hasErr = true;
	bakLn[1] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _memory.parts;
		try {
			t = matchBNFWord("1", line);
			_memory.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_memory.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[1];
		bakParts = _memory.parts;
		try {
			t = matchBNFWord("2", line);
			_memory.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_memory.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[1];
		myE.info = "mismatch in MEMORY";
		myE.line = line;
		throw myE;
	}
	return _memory;
}

TTerm match_command(string& line) {
	TTerm _command;
	TTerm t;
	_command.type = COMMAND;

	bool hasErr = false;
	MyException myE;
	string bakLn[10];
	vector<TTerm> bakParts;

	hasErr = true;
	bakLn[0] = line;
	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _command.parts;
		try {
			t = matchBNFWord("TRADE", line);
			_command.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_command.parts = bakParts;
		}
	}

	if (hasErr) {
		hasErr = false;
		line = bakLn[0];
		bakParts = _command.parts;
		try {
			t = matchBNFWord("CHEAT", line);
			_command.parts.push_back(t);

		}
		catch(MyException e){
			hasErr = true;
			myE = e;
			_command.parts = bakParts;
		}
	}

	if (hasErr) {
		line = bakLn[0];
		myE.info = "mismatch in COMMAND";
		myE.line = line;
		throw myE;
	}
	return _command;
}

const int COMMAND_NULL = 0;
const int COMMAND_TRADE = 1;
const int COMMAND_CHEAT = 2;

int last[2][3];

int eval(TTerm x) {
	int res1,res2;

	switch (x.type) {
	case PROGRAM:
	case STATEMENT:
		return eval(x.parts[0]);
		break;
	case IFSTAT: {
			res1 = eval(x.parts[1]);
			if (res1==0) return eval(x.parts[5]);
			else return eval(x.parts[3]);
			break;
		}
	case CONDITION:
		if (x.parts.size() == 1) {
			return eval(x.parts[0]);
		}
		else {
			int res1 = eval(x.parts[0]);
			if (x.parts[1].parts[0].value == "AND") {
				if (res1 == 0) return 0;
			}
			else  {
				if (res1 == 1) return 1;
			}
			return eval(x.parts[2]);
		}
		break;
	case COND:
		res1 = eval(x.parts[0]);
		if (x.parts[2].type == BNFWORD)
			res2 = (x.parts[2].value == "NULL")?COMMAND_NULL:-1;
		else
			res2 = eval(x.parts[2]);
		if (x.parts[1].value == "=") 
			return res1 == res2;
		else 
			return res1 != res2;
		break;
	case MEMORY:
		if (x.parts[0].value == "MY") res1 = 0;
		else res1 = 1;
		if (x.parts[2].value == "1") res2 = 0;
		else res2 = 1;
		return last[res1][res2];
		break;
	case COMMAND:
		if (x.parts[0].value == "TRADE") return COMMAND_TRADE;
		else if (x.parts[0].value == "CHEAT") return COMMAND_CHEAT;
		else return COMMAND_NULL;
		break;
	}

	return -1;
}

int against(TTerm a, TTerm b) {
	int myDecision = eval(a);
	for (int i=0; i<3; ++i) {
		swap(last[0][i], last[1][i]);
	}
	int yourDecision = eval(b);
	for (int i=0; i<3; ++i) {
		swap(last[0][i], last[1][i]);
	}
// 	cout << myDecision << " " << yourDecision << endl;
	last[0][1] = last[0][0];
	last[1][1] = last[1][0];
	last[0][0] = myDecision;
	last[1][0] = yourDecision;
	if (myDecision == COMMAND_CHEAT && yourDecision == COMMAND_CHEAT)
		return -1;
	if (myDecision == COMMAND_TRADE && yourDecision == COMMAND_TRADE)
		return 1;
	if (myDecision == COMMAND_TRADE)
		return -2;
	else
		return 2;
}

int main(void)
{
	char line[300];
	string program="";
	TTerm progT[20];
	int idx=0;
	while (true) {
		scanf(" ");
		cin.getline(line, 300);
		if (line[0] == '#') break;
		string sl(line);
		bool end = false;
		while (sl.length()>0) {
			for (unsigned int i=0; i<sl.length(); ++i) {
				if (sl[i] == '.') {
					program = program + sl.substr(0, i+1);
					end = true;
					sl = sl.substr(i+1, sl.length()-i-1);
					break;
				}
			}
			if (end) {
				end = false;
				progT[idx++] = match_program(program);
				program = "";
			}
			else {
				program = program + sl;
				sl = "";
			}
		}
	}

	for (int i=0; i<idx; ++i) {
		int score = 0;
		for (int j=0; j<idx; ++j) {
			if (i==j) continue;
			else {
				memset(last, 0, 2*3*sizeof(int));
				for (int k=0; k<10; ++k)
					score += against(progT[i], progT[j]);
			}
		}
		printf("%3d\n", score);
	}

	return 0;
}
